<?php
// 应用公共文件

if (!function_exists('format_time')) {
    /**
     * @param string $format 格式化内容
     * @param null $time 数值型数据
     * @return false|string
     */
    function format_time($format = 'Y-m-d H:i:s', $time = null)
    {
        $time = $time ?: time();
        $result = is_numeric($time) ? date($format, $time) : date($format, strtotime($time));
        return $result;

    }
}
if (!function_exists('password_make_or_verify')) {
    /**
     * @param string $password 需要加密或校验的密码
     * @param string $checkPassword 需要检测的密码
     * @param        $algo 一个用来在散列密码时指示算法的密码算法常量
     * @return bool|string 存在校验密码时返回bool 否则返回 string
     */
    function password_make_or_verify(string $password, string $checkPassword = '')
    {
        if ($checkPassword !== '') {
            return password_verify($password, $checkPassword);
        } else {
            return password_hash($password);
        }
    }
}

if (!function_exists('chat_center')) {
    /**
     * @param int|float $fontsize 需要局中的文字的字体大小
     * @param string $font 需要局中的文字的字体URL
     * @param string $content 需要局中的文字
     * @param int|float $maxLength 大背景的宽度|高度
     * @param int|float $type 类型 1 为宽度 2 为高度
     * @return float
     * @author zsj 2019-05-13
     */
    function chat_center($fontsize, $font, $content, $maxLength, $type = 1)
    {
        $array = imagettfbbox($fontsize, 0, $font, $content);
        if ($type == 1) {
            return ceil(($maxLength - $array[2] - $array[0]) / 2);
        } else {
            return ceil(($maxLength - $array[1] - $array[7]) / 2);
        }
    }

}
if (!function_exists('rmdirs')) {

    /**
     * 删除文件夹
     * @param string $dirname 目录
     * @param bool $withself 是否删除自身
     * @return boolean
     */
    function rmdirs($dirname, $withself = true)
    {
        if (!is_dir($dirname)) {
            return false;
        }
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }
        if ($withself) {
            @rmdir($dirname);
        }
        return true;
    }
}
if (!function_exists('assign_config')) {
    function assign_config($name, $value = ''): void
    {
        $view = \think\facade\View::instance();
        $view->config = array_merge($view->config ?: [], is_array($name) ? $name : [$name => $value]);
    }
}
if (!function_exists('build_suffix_image')) {
    /**
     * 生成文件后缀图片
     * @param string $suffix 后缀
     * @param null $background
     * @return string
     */
    function build_suffix_image($suffix, $background = null)
    {
        $suffix = mb_substr(strtoupper($suffix), 0, 4);
        $total = unpack('L', hash('adler32', $suffix, true))[1];
        $hue = $total % 360;
        list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);

        $background = $background ? $background : "rgb({$r},{$g},{$b})";

        $icon = <<<EOT
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
            <path style="fill:#E2E5E7;" d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z"/>
            <path style="fill:#B0B7BD;" d="M384,128h96L352,0v96C352,113.6,366.4,128,384,128z"/>
            <polygon style="fill:#CAD1D8;" points="480,224 384,128 480,128 "/>
            <path style="fill:{$background};" d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z"/>
            <path style="fill:#CAD1D8;" d="M400,432H96v16h304c8.8,0,16-7.2,16-16v-16C416,424.8,408.8,432,400,432z"/>
            <g><text><tspan x="220" y="380" font-size="124" font-family="Verdana, Helvetica, Arial, sans-serif" fill="white" text-anchor="middle">{$suffix}</tspan></text></g>
        </svg>
EOT;
        return $icon;
    }
}

if (!function_exists('hsv2rgb')) {
    function hsv2rgb($h, $s, $v)
    {
        $r = $g = $b = 0;

        $i = floor($h * 6);
        $f = $h * 6 - $i;
        $p = $v * (1 - $s);
        $q = $v * (1 - $f * $s);
        $t = $v * (1 - (1 - $f) * $s);

        switch ($i % 6) {
            case 0:
                $r = $v;
                $g = $t;
                $b = $p;
                break;
            case 1:
                $r = $q;
                $g = $v;
                $b = $p;
                break;
            case 2:
                $r = $p;
                $g = $v;
                $b = $t;
                break;
            case 3:
                $r = $p;
                $g = $q;
                $b = $v;
                break;
            case 4:
                $r = $t;
                $g = $p;
                $b = $v;
                break;
            case 5:
                $r = $v;
                $g = $p;
                $b = $q;
                break;
        }

        return [
            floor($r * 255),
            floor($g * 255),
            floor($b * 255)
        ];
    }
}
if (!function_exists('format_date')) {
    function format_date($time)
    {
        $old = $time;
        $time = is_numeric($time) === false ? strtotime($time) : $time;
        $t = time() - $time;
        if ($t <= 0) {
            return '刚刚';
        } elseif ($t > 86400) {
            return $old;
        }
        $f = [
            '31536000' => '年',
            '2592000'  => '个月',
            '604800'   => '星期',
            '86400'    => '天',
            '3600'     => '小时',
            '60'       => '分钟',
            '1'        => '秒'
        ];
        foreach ($f as $k => $v) {
            if (0 != $c = floor($t / (int)$k)) {
                return $c . $v . '前';
            }
        }
    }
}

